//
//  KZProductDetailVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/26.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZProductDetailVC: KZBaseViewControllerWithBack {
    
    let table = UITableView()
    var header: KZProductDetailTableHeader!
    let toolbar = KZProdectDetailToolBar()

    
    deinit {
        print("销毁详情页")
        header.banner.timer!.invalidate()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "商品详情"
        setupUI()
    }
    
    override func viewSafeAreaInsetsDidChange() {
        if #available(iOS 11.0, *) {
            super.viewSafeAreaInsetsDidChange()
            toolbar.snp.updateConstraints { (m) in
                m.bottom.equalTo(-view.safeAreaInsets.bottom)
            }
        }
    }
    
   
}

// MARK: ------ 创建视图
extension KZProductDetailVC {
 
    func setupUI() {
        
        view.addSubview(toolbar)
        toolbar.snp.makeConstraints { (m) in
            m.left.bottom.right.equalTo(0)
            m.height.equalTo(49)
        }
        
        table.backgroundColor = kBgColor
        table.separatorStyle = .none
        table.estimatedRowHeight = 200
        table.rowHeight = UITableViewAutomaticDimension
        table.tableFooterView = UIView()
        table.delegate = self
        table.dataSource = self
        self.view.addSubview(table)
        table.snp.makeConstraints { (m) in
            m.left.top.right.equalTo(0)
            m.bottom.equalTo(toolbar.snp.top)
        }
        table.register(KZProductDetailInformationCell.self, forCellReuseIdentifier: "informationCell")
        table.register(KZProductDetailDescriptionCell.self, forCellReuseIdentifier: "descriptionCell")
        
        header = KZProductDetailTableHeader(frame: CGRect(x: 0, y: 0, width: view.width, height: view.width))
        table.tableHeaderView = header
    }
}

// MARK: ------ tableview datasource
extension KZProductDetailVC: UITableViewDelegate, UITableViewDataSource {
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "informationCell") as! KZProductDetailInformationCell
            
            return cell
        }
        else if indexPath.section == 1 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "descriptionCell") as! KZProductDetailDescriptionCell
            
            return cell
        }
        
        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 0 : 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
}
